package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbGroupActiveMapper;
import com.bwie.mapper.TbGroupMapper;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbGroup;
import com.bwie.pojo.TbGroupActive;
import com.bwie.pojo.TbGroupJoin;
import com.bwie.service.TbGroupJoinService;
import com.bwie.mapper.TbGroupJoinMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 叶雨洁
* @description 针对表【tb_group_join】的数据库操作Service实现
* @createDate 2025-07-27 19:14:38
*/
@Service
public class TbGroupJoinServiceImpl extends ServiceImpl<TbGroupJoinMapper, TbGroupJoin>
    implements TbGroupJoinService{

    @Autowired
    TbGroupJoinMapper tbGroupJoinMapper;

    @Autowired
    TbGroupActiveMapper tbGroupActiveMapper;

    @Autowired
    TbGroupMapper groupMapper;
    @Override
    public R TeamJoinPageList(PageInfoVo pageInfoVo) {

        Page<TbGroupJoin> tbGroupJoinPage = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
        QueryWrapper<TbGroupJoin> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            queryWrapper.lambda().like(TbGroupJoin::getJoinName,pageInfoVo.getKeyWord());
        }
        if(pageInfoVo.getStartTime()!=null  ||pageInfoVo.getEndTime()!=null){
            queryWrapper.lambda().between(TbGroupJoin::getBeginTime,pageInfoVo.getStartTime(),pageInfoVo.getEndTime());
        }
        queryWrapper.lambda().orderByDesc(TbGroupJoin::getBeginTime);
        Page<TbGroupJoin> tbGroupJoinPage1 = tbGroupJoinMapper.selectPage(tbGroupJoinPage, queryWrapper);
        return R.OK(tbGroupJoinPage1);
    }

    @Override
    public R instr(TbGroupJoin groupJoin) {

        TbGroupJoin tbGroupJoin = tbGroupJoinMapper.selectOne(
                new QueryWrapper<TbGroupJoin>().lambda().eq(TbGroupJoin::getJoinName, groupJoin.getJoinName())
        );
        if(tbGroupJoin!=null){
            return R.ERROR(500,"拼团活动已存在");
        }
        tbGroupJoinMapper.insert(groupJoin);
        return R.OK();
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public R deleteJoinActive(Integer joinId) {

        try {
            TbGroupJoin tbGroupJoin = tbGroupJoinMapper.selectById(joinId);

            //删除拼团
            TbGroup tbGroup = groupMapper.selectOne(
                    new QueryWrapper<TbGroup>().lambda().eq(TbGroup::getJoinId, joinId)
            );
            if(tbGroup==null){
                return R.ERROR(500,"没有此拼团");
            }

            groupMapper.deleteById(tbGroup.getGroupId());

            //判断是否有这个活动
            TbGroupActive tbGroupActive = tbGroupActiveMapper.selectOne(
                    new QueryWrapper<TbGroupActive>().lambda().eq(TbGroupActive::getActiveId, tbGroupJoin.getActiveId())
            );
            if(tbGroupActive==null){
                return R.ERROR(500,"活动不存在");
            }
            //删除参与活动
            tbGroupJoinMapper.deleteById(joinId);
            //删除拼团活动
            tbGroupActiveMapper.deleteById(tbGroupActive.getActiveId());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.OK();
    }
}




